package com.ruoyi.web.controller.mes;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.web.domain.vo.CareDetailVo;
import com.ruoyi.web.domain.vo.WasteVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.mes.MesWaste;
import com.ruoyi.web.service.IMesWasteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 废料处理Controller
 * 
 * @author levicash
 * @date 2024-01-23
 */
@RestController
@RequestMapping("/mes/waste")
public class MesWasteController extends BaseController
{
    @Autowired
    private IMesWasteService mesWasteService;

    /**
     * 查询废料处理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MesWaste mesWaste)
    {
        startPage();
        List<MesWaste> list = mesWasteService.selectMesWasteList(mesWaste);
        return getDataTable(list);
    }

    //结算：查询结算明细
    @GetMapping("/listJS")
    public TableDataInfo listJS(MesWaste mesWaste)
    {
        startPage();
        List<MesWaste> list = mesWasteService.selectMesWasteListJS(mesWaste);
        return getDataTable(list);
    }

    /**
     * 导出废料处理列表
     */
    @Log(title = "废料处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesWaste mesWaste)
    {
        List<MesWaste> list = mesWasteService.selectMesWasteList(mesWaste);
        ExcelUtil<MesWaste> util = new ExcelUtil<MesWaste>(MesWaste.class);
        util.exportExcel(response, list, "废料处理数据");
    }

    /**
     * 获取废料处理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mesWasteService.selectMesWasteById(id));
    }

    /**
     * 新增废料处理
     */
    @Log(title = "废料处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesWaste mesWaste)
    {
        return toAjax(mesWasteService.insertMesWaste(mesWaste));
    }

    /**
     * 修改废料处理
     */
    @Log(title = "废料处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesWaste mesWaste)
    {
        return toAjax(mesWasteService.updateMesWaste(mesWaste));
    }

    /**
     * 删除废料处理
     */
    @Log(title = "废料处理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mesWasteService.deleteMesWasteByIds(ids));
    }

    /**
     * 查询废料处理明细列表
     */
    @GetMapping("/detail")
    public TableDataInfo detail(WasteVo wasteVo) {
        startPage();
        Map<String, Object> map = new HashMap<>();
        map.put("wasteCode", wasteVo.getWasteCode());
        map.put("name", wasteVo.getName());
        map.put("handleDept", wasteVo.getHandleDept());
        map.put("beginTime", wasteVo.getBeginTime());
        map.put("endTime", wasteVo.getEndTime());
        map.put("status", wasteVo.getStatus());
        List<WasteVo> list = mesWasteService.selectMesWasteDetailList(map);
        return getDataTable(list);
    }

    /**
     * 导出废料处理明细列表
     */
    @Log(title = "废料处理明细", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDetail")
    public void exportDetail(HttpServletResponse response, WasteVo wasteVo)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("wasteCode", wasteVo.getWasteCode());
        map.put("name", wasteVo.getName());
        map.put("handleDept", wasteVo.getHandleDept());
        map.put("beginTime", wasteVo.getBeginTime());
        map.put("endTime", wasteVo.getEndTime());
        List<WasteVo> list = mesWasteService.selectMesWasteDetailList(map);
        // 定义合计字段
        BigDecimal quantity = new BigDecimal(0); // 数量
        BigDecimal weightTare = new BigDecimal(0); // 皮重
        BigDecimal weightGross = new BigDecimal(0); // 毛重
        BigDecimal weightNet = new BigDecimal(0); // 净重
        BigDecimal amount = new BigDecimal(0); // 金额
        for (WasteVo vo : list) {
            quantity = quantity.add(vo.getQuantity() == null ? new BigDecimal(0) : vo.getQuantity());
            weightTare = weightTare.add(vo.getWeightTare() == null ? new BigDecimal(0) : vo.getWeightTare());
            weightGross = weightGross.add(vo.getWeightGross() == null ? new BigDecimal(0) : vo.getWeightGross());
            weightNet = weightNet.add(vo.getWeightNet() == null ? new BigDecimal(0) : vo.getWeightNet());
            amount = amount.add(vo.getAmount() == null ? new BigDecimal(0) : vo.getAmount());
        }
        String s = quantity.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
        String s1 = weightTare.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
        String s2 = weightGross.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
        String s3 = weightNet.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
        String s4 = amount.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
        WasteVo wasteVo1 = new WasteVo();
        wasteVo1.setQuantity(new BigDecimal(s));
        wasteVo1.setWeightTare(new BigDecimal(s1));
        wasteVo1.setWeightGross(new BigDecimal(s2));
        wasteVo1.setWeightNet(new BigDecimal(s3));
        wasteVo1.setAmount(new BigDecimal(s4));
        list.add(wasteVo1);
        ExcelUtil<WasteVo> util = new ExcelUtil<WasteVo>(WasteVo.class);
        util.exportExcel(response, list, "废料处理明细数据");
    }
}
